package com.forcedotcom
{
	import flashx.textLayout.elements.BreakElement;
	import flashx.textLayout.elements.LinkElement;
	import flashx.textLayout.elements.ParagraphElement;
	import flashx.textLayout.elements.SpanElement;
	import flashx.textLayout.elements.TextFlow;
	import flashx.textLayout.events.FlowElementMouseEvent;

	public class PostParser
	{
		
		public function PostParser()
		{
		}
		
		public static function parseComment(comment:Object, connection:AccountConnection, linkAction:Function):TextFlow {
			var tf:TextFlow = new TextFlow();
			var p:ParagraphElement = new ParagraphElement();
			p.addChild(nameToLink(comment.CreatedBy.Name, comment.CreatedById, connection, linkAction));
			p.addChild(textToSpan(" " + cleanString(comment.CommentBody)));
			tf.addChild(p);
			tf.linkNormalFormat = { "textDecoration":"none", "fontWeight":"bold", "color":"#d3eef9" };
			return tf;
		}
		public static function parsePost(post:Object, connection:AccountConnection, linkAction:Function):TextFlow {
			var str:String = "";
			var tf:TextFlow = new TextFlow();
			switch (post.FeedPost.Type) {
				case "TextPost":
					tf = parseTextPost(post, connection, linkAction);
					break;
				case "UserStatus":
					tf = parseUserStatusPost(post, connection, linkAction);
					break;
				case "ContentPost":
					tf = parseContentPost(post, connection, linkAction);
					break;
				case "LinkPost":
					tf = parseLinkPost(post, connection, linkAction);
					break;
				default:
					trace(post.FeedPost.Type);
					break;
			}
			tf.linkNormalFormat = { "textDecoration":"none", "fontWeight":"bold", "color":"#d3eef9" };
			
			return tf;
		}
		
		private static function textToSpan(txt:String):SpanElement {
			var sp:SpanElement = new SpanElement();
			sp.text = txt;
			return sp;
		}
		private static function parseTextPost(post:Object, connection:AccountConnection, linkAction:Function):TextFlow {
			var p:ParagraphElement = new ParagraphElement();
			p.addChild(textToSpan(post.Type));
			p.addChild(new BreakElement());
			p.addChild(nameToLink(post.CreatedBy.Name, post.CreatedById, connection, linkAction));
			if (post.CreatedById != post.ParentId) {
				p.addChild(textToSpan(" to "));
				p.addChild(nameToLink(post.Parent.Name, post.ParentId, connection, linkAction));
				p.addChild(textToSpan(":"));
			}
			p.addChild(textToSpan(" "));
			p.addChild(textToSpan(cleanString(post.FeedPost.Body)));
			var tf:TextFlow = new TextFlow();
			tf.addChild(p);
			return tf;
		}
		
		private static function parseUserStatusPost(post:Object, connection:AccountConnection, linkAction:Function):TextFlow {
			var p:ParagraphElement = new ParagraphElement();
			p.addChild(textToSpan(post.Type));
			p.addChild(new BreakElement());
			p.addChild(nameToLink(post.Parent.Name, post.ParentId, connection, linkAction));
			p.addChild(textToSpan(" "));
			if (post.FeedPost.Title != null) {
				p.addChild(textToSpan(cleanString(post.FeedPost.Title)));
			}
			p.addChild(textToSpan(cleanString(post.FeedPost.Body)));
			var tf:TextFlow = new TextFlow();
			tf.addChild(p);
			return tf;
		}

		private static function parseContentPost(post:Object, connection:AccountConnection, linkAction:Function):TextFlow {
			var p:ParagraphElement = new ParagraphElement();
			p.addChild(textToSpan(post.Type));
			p.addChild(new BreakElement());
			p.addChild(nameToLink(post.CreatedBy.Name, post.CreatedById, connection, linkAction));
			p.addChild(textToSpan(" " + cleanString(post.FeedPost.Title)));
			var tf:TextFlow = new TextFlow();
			tf.addChild(p);
			return tf;
		}

		private static function parseLinkPost(post:Object, connection:AccountConnection, linkAction:Function):TextFlow {
			var p:ParagraphElement = new ParagraphElement();
			p.addChild(textToSpan(post.Type));
			p.addChild(new BreakElement());
			p.addChild(nameToLink(post.CreatedBy.Name, post.CreatedById, connection, linkAction));
			if (post.CreatedBy.Name != post.Parent.Name) { 
				p.addChild(textToSpan(" to "));
				p.addChild(nameToLink(post.Parent.Name, post.ParentId, connection, linkAction));
			}
			p.addChild(textToSpan(": "));
			if (post.FeedPost.Body != null) {
				p.addChild(textToSpan(cleanString(post.FeedPost.Body)));
			}
			p.addChild(new BreakElement());
			p.addChild(linkToLink(post.FeedPost.Title, "", connection, linkAction));
			var tf:TextFlow = new TextFlow();
			tf.addChild(p);
			return tf;
		}

		private static function linkToLink(name:String, id:String, connection:AccountConnection, linkAction:Function):LinkElement {
			var le:LinkElement = new LinkElement();
			var baseUrl:String = connection.serverUrlBase + "/secur/frontdoor.jsp?sid=" + connection.loginResult.sessionId + "&retURL=/";
			var createdByRetUrl:String = id;
			le.href = baseUrl + createdByRetUrl;
			le.target = "_blank";
			le.addEventListener(FlowElementMouseEvent.CLICK, linkAction);
			var sp:SpanElement = new SpanElement();
			sp.text = name;
			le.addChild(sp);
			return le;
		}
		
		private static function nameToLink(name:String, id:String, connection:AccountConnection, linkAction:Function):LinkElement {
			var baseUrl:String = connection.serverUrlBase + "/secur/frontdoor.jsp?sid=" + connection.loginResult.sessionId + "&retURL=/";
			var createdByRetUrl:String = id;
			var le:LinkElement = new LinkElement();
			le.href = baseUrl + createdByRetUrl;
			le.target = "_blank";
			le.addEventListener(FlowElementMouseEvent.CLICK, linkAction);
			var sp:SpanElement = new SpanElement();
			sp.text = name;
			le.addChild(sp);
			return le;
		}

		private static function cleanString(str:String):String {
			str = str.replace(new RegExp("<", "g"), "&lt;");
			str = str.replace(new RegExp(">", "g"), "&gt;");
			return str;
		}

	}
}